#pragma once

#include <ctemplate/template.h>
#include <iostream>
#include <string>
#include <vector>

namespace OJ
{
  const std::string template_path = "./template_html/";
  class View
  {
  public:
    View() = default;
    virtual ~View() = default;

  public:
    void creatAllQuestionsHTML(std::vector<OJ::Question> &questions, std::string &html)
    {
      std::string path = template_path + "all_questions.html";
      //创建数据字典
      ctemplate::TemplateDictionary root("all_questions");
      for (auto &question : questions)
      {
        //创建子数据字典
        ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
        //设置子数据字典的值
        sub->SetValue("number", std::to_string(question.question_number));
        sub->SetValue("title", question.title);
        sub->SetValue("star", question.difficulty);
      }
      //获取模板
      ctemplate::Template *template_ =  ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
      //执行渲染
      template_->Expand(&html, &root);
    }
    void creatQuestionHTML(OJ::Question& question, std::string &html)
    {
      std::string path = template_path + "one_question.html";
      //创建数据字典
      ctemplate::TemplateDictionary root("one_question");
      //设置数据字典的值
      root.SetValue("number", std::to_string(question.question_number));
      root.SetValue("title", question.title);
      root.SetValue("star", question.difficulty);
      root.SetValue("description", question.description);
      root.SetValue("cpu_limit", std::to_string(question.cpu_limit));
      root.SetValue("memory_limit", std::to_string(question.memory_limit));
      root.SetValue("header", question.header);
      //获取模板
      ctemplate::Template *template_ =  ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
      //执行渲染
      template_->Expand(&html, &root);
    }
  };
}